package com.ztc.ZtcCapitalIncome.web;


import com.aqgj.User.bean.User;
import com.aqgj.util.JsonWrapper;
import com.ztc.ZtcCapitalIncome.service.ZtcCapitalIncomeManager;
import com.ztc.ZtcTransport.bean.ZtcTransport;
import com.ztc.ZtcTransport.service.ZtcTransportManager;
import com.ztc.util.excel.ExcelUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.*;


@Controller
@RequestMapping("ztc/capitalIncome")
public class ZtcCapitalIncomeAction {

    @Autowired
    private ZtcCapitalIncomeManager ztcCapitalIncomeManager;
    @Autowired
    private ZtcTransportManager ztcTransportManager;

    /**
     * 获取平台收益列表.
     * @param session
     * @param pageNumber 页码
     * @param pageSize  每页大小
     * @return
     */
    @ResponseBody
    @RequestMapping("getZtcCapitalIncomeManager")
    public HashMap<String,Object> getZtcCapitalIncomeManager(HttpSession session, @RequestParam(defaultValue ="1") Integer pageNumber, @RequestParam(defaultValue ="10") Integer pageSize, String startDate, String endDate){
        User user=(User)session.getAttribute("userInfo");
        ZtcTransport ztcTransport=ztcTransportManager.get(user.getTransportId());
        if(ztcTransport==null){
            return JsonWrapper.failureWrapperMsg("公司信息数据异常！");
        }
//        if(ztcTransport.getType()!=0){
//            return JsonWrapper.failureWrapperMsg("无法查看，您的权限不足！");
//        }
        return  ztcCapitalIncomeManager.searchCapitalIncome(pageNumber,pageSize,startDate,endDate);
    }

    /**
     * 导出excel
     * @param response
     */
    @RequestMapping(value = "export")
    public void exportFeedBack(HttpServletResponse response, HttpSession session, @RequestParam(defaultValue ="1") Integer pageNumber, @RequestParam(defaultValue ="10") Integer pageSize, String startDate, String endDate) {
        User user=(User)session.getAttribute("userInfo");
        //文件名
        String fileName = "平台收益明细"+System.currentTimeMillis() + ".xls";
        //sheet名
        String sheetName = "平台收益明细";
        //标题
        String[] title = new String[]{"订单id", "业务类型", "业务金额", "收益金额", "备注", "时间"};
       HashMap<String,Object> map= ztcCapitalIncomeManager.searchCapitalIncome(pageNumber,pageSize,startDate,endDate);
        List<Map<String, Object>> list = (List<Map<String,Object>>) map.get("data");

        String[][] values = new String[list.size()][];
        for (int i = 0; i < list.size(); i++) {
            values[i] = new String[title.length];
            //将对象内容转换成string
            Map<String, Object> obj = list.get(i);
            values[i][0] = obj.get("orderId").toString();
            values[i][1] = ExcelUtil.getCapitalIncomeType(Integer.valueOf(obj.get("type").toString()));
            values[i][2] = obj.get("money").toString();
            values[i][3] = obj.get("incomeMoney").toString();
            values[i][4] = obj.get("remark").toString();
            values[i][5] = obj.get("createTime").toString();
        }

        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, values, null);
        ExcelUtil.setResponseHeader(response,fileName,wb);
    }

    /**
     * 导出excel全部数据
     * @param response
     */
    @RequestMapping("exportAllExcel")
    public void exportAllExcel(HttpServletResponse response, HttpSession session) {
        User user=(User)session.getAttribute("userInfo");
        //文件名
        String fileName = "平台收益明细"+System.currentTimeMillis() + ".xls";
        //sheet名
        String sheetName = "平台收益明细";
        //标题
        String[] title = new String[]{"订单ID", "业务类型", "业务金额", "收益金额", "备注", "时间"};
        List<Object[]> list= ztcCapitalIncomeManager.getAllCapitalIncome();
        String[][] values = new String[list.size()][];
        for (int i = 0; i < list.size(); i++) {
            values[i] = new String[title.length];
            //将对象内容转换成string
            Object[] obj = list.get(i);
            values[i][0] = obj[0].toString();
            values[i][1] = ExcelUtil.getCapitalIncomeType(Integer.valueOf(obj[1].toString()));
            values[i][2] = obj[2].toString();
            values[i][3] = obj[3].toString();
            values[i][4] = obj[4].toString();
            values[i][5] = obj[5].toString();
        }

        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, values, null);
        ExcelUtil.setResponseHeader(response,fileName,wb);
    }

}